﻿using AutoMapper;
using AutoMapperApp.Dtos;
using AutoMapperApp.Models;
using AutoMapperApp.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.JSInterop.Infrastructure;
using System.Collections.Generic;

namespace AutoMapperApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        private readonly DataRepository _dataRepository;
        private readonly IMapper _mapper;
        public StudentController(DataRepository dataRepository,
           IMapper mapper)
        {
            _dataRepository = dataRepository;
            _mapper = mapper;
        }
        [HttpGet]
        public async Task<List<StudentDto>> Get()
        {

            List<StudentModel> modelList = new List<StudentModel>() {
                new StudentModel(){
                    ID = 1,
                    Name="张三",
                    Password="123",
                    Age=10,
                    Gender="男"
                },
                new StudentModel(){
                    ID = 2,
                    Name="李四",
                    Password="456",
                    Age=11,
                    Gender="女"
                },
                new StudentModel(){
                    ID = 3,
                    Name="王五",
                    Password="789",
                    Age=12,
                    Gender="男"
                },
                new StudentModel(){
                    ID = 4,
                    Name="赵六",
                    Password="111213",
                    Age=13,
                    Gender="男"
                },
                new StudentModel(){
                    ID = 5,
                    Name="邹七",
                    Password="141516",
                    Age=14,
                    Gender="女"
                },
            }; 
            List<StudentDto> dtoList = _mapper.Map<List<StudentDto>>(modelList);



            StudentModel stu = new StudentModel()
            {
                ID = 1,
                Name = "张三",
                Gender = "男",
                Age = 18,
                Password = "123"
            };



            //StudentDto dto = new StudentDto();
            //dto.ID = stu.ID;
            //dto.Name = stu.Name;
            //dto.Age = stu.Age;
            //dto.Gender = stu.Gender;




            // 循环给属性赋值
            //foreach (var item in list)
            //{
            //    StudentDto dto = new StudentDto();
            //    dto.ID = item.ID;
            //    dto.Name = item.Name;
            //    dto.Age = item.Age;
            //    dto.Gender = item.Gender;
            //    // 加入到集合中
            //    dtoList.Add(dto);
            //}
            return null;
        }
    }
}
